<?php
require_once ("BaseDAO.php");

// InventoryItem Value Object
class InventoryItem extends BaseVO {
	var $id;
	var $title;
	var $description;
	var $available;
	var $initial_qty;
	var $inventory_type;

	// create a new VO
	function InventoryItem(
		$id= 0,
		$title= "",
		$description= "",
		$available= "",
		$initial_qty= 0,
		$inventory_type= 0
	) {
		$this->id = $id;
		$this->title = $title;
		$this->description = $description;
		$this->available = $available;
		$this->initial_qty = $initial_qty;
		$this->inventory_type = $inventory_type;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->title == $vo->title &&
			$this->description == $vo->description &&
			$this->available == $vo->available &&
			$this->initial_qty == $vo->initial_qty &&
			$this->inventory_type == $vo->inventory_type;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->title = $vo->title;
		$this->description = $vo->description;
		$this->available = $vo->available;
		$this->initial_qty = $vo->initial_qty;
		$this->inventory_type = $vo->inventory_type;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->title . "," . $this->description . "," . $this->available . "," . $this->initial_qty . "," . $this->inventory_type;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<title>$this->title</title>\n" .
		"<description>$this->description</description>\n" .
		"<available>$this->available</available>\n" .
		"<initial_qty>$this->initial_qty</initial_qty>\n" .
		"<inventory_type>$this->inventory_type</inventory_type>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->title = $this->formHelper($prefix . "title", "");
		$this->description = $this->formHelper($prefix . "description", "");
		$this->available = $this->formHelper($prefix . "available", "");
		$this->initial_qty = $this->formHelper($prefix . "initial_qty", 0);
		$this->inventory_type = $this->formHelper($prefix . "inventory_type", 0);
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->title = $this->queryHelper($prefix . "title", "");
		$this->description = $this->queryHelper($prefix . "description", "");
		$this->available = $this->queryHelper($prefix . "available", "");
		$this->initial_qty = $this->queryHelper($prefix . "initial_qty", 0);
		$this->inventory_type = $this->queryHelper($prefix . "inventory_type", 0);
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// InventoryItem Data Access Object
class InventoryItemDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `inventory_item` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `inventory_item` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `inventory_item` (title,description,available,initial_qty,inventory_type) VALUES ('%A','%B','%C',%D,%E)";
	var $SQL_UPDATE = "UPDATE `inventory_item` SET ";
	var $SQL_DELETE = "DELETE FROM `inventory_item` WHERE id=%A";

	// default constructor
	function InventoryItemDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single InventoryItem obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new InventoryItem(
				$row->id, 
				$row->title, 
				$row->description, 
				$row->available, 
				$row->initial_qty, 
				$row->inventory_type
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of InventoryItem objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new InventoryItem(
				$row->id, 
				$row->title, 
				$row->description, 
				$row->available, 
				$row->initial_qty, 
				$row->inventory_type
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of InventoryItem objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new InventoryItem(
				$row->id, 
				$row->title, 
				$row->description, 
				$row->available, 
				$row->initial_qty, 
				$row->inventory_type
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of InventoryItem objects based on the example
	function findExample($example, $orderby="") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if(strlen($example->title) > 0) $tmp .= "title like '%" . $this->safeEscapeStr($example->title) . "%' and ";
		if(strlen($example->description) > 0) $tmp .= "description like '%" . $this->safeEscapeStr($example->description) . "%' and ";
		if(strlen($example->available) > 0) $tmp .= "available = DATE('" . $this->safeEscapeStr($example->available) . "') and ";
		if($example->initial_qty != 0) $tmp .= "initial_qty = " . $this->safeEscapeStr($example->initial_qty) . " and ";
		if($example->inventory_type != 0) $tmp .= "inventory_type = " . $this->safeEscapeStr($example->inventory_type) . " and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new InventoryItem(
				$row->id, 
				$row->title, 
				$row->description, 
				$row->available, 
				$row->initial_qty, 
				$row->inventory_type
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->title), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->description), $this->sql);
		$this->sql = str_replace("%C", $this->safeEscapeStr($vo->available), $this->sql);
		$this->sql = str_replace("%D", $this->safeEscapeStr($vo->initial_qty), $this->sql);
		$this->sql = str_replace("%E", $this->safeEscapeStr($vo->inventory_type), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "title = '" . $this->safeEscapeStr($vo->title) . "', ";
		$this->sql .= "description = '" . $this->safeEscapeStr($vo->description) . "', ";
		$this->sql .= "available = '" . $this->safeEscapeStr($vo->available) . "', ";
		$this->sql .= "initial_qty = " . $this->safeEscapeStr($vo->initial_qty) . ", ";
		$this->sql .= "inventory_type = " . $this->safeEscapeStr($vo->inventory_type) . " ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

